import { useEffect, useRef, useState } from "react"
import HomePage_ModalComponent from "../../../../component/modal"
import HomePage_TableComponent from "../../../../component/table"
import { Divider, message } from "antd"
import HomePage_DataMaintenancePage_UprightMaintenancePage_ModalComponent from "./component/modal"
import { $eventBus, $EventBus_off, $EventBus_on, $export_excel, $PowerSetting } from "../../../../../../common/utils"
import HomePage_ModalComponent_ConfirmationModallComponent from "../../../../component/modal/component/confirmationModal"
import { $Http } from "../../../../../../network"
import dayjs from 'dayjs';
// 主页-数据维护页面-隔直维护页面
const HomePage_DataMaintenancePage_UprightMaintenancePage = () => {
    const initParamsData = useRef<object>({})
    const paramsData = useRef<object>({})
    let [pageNo, setPageNo] = useState<number>(1)
    let [pageSize, setPageSize] = useState<number>(10)
    const [sourceData, setSourceData] = useState<object>({})
    const [isModal, setIsModal] = useState<boolean>(false)
    const [isDeleteModal, setIsDeleteModal] = useState<boolean>(false)
    const [modalData, setModalData] = useState<object>({})

    const columnList = [
        {
            title: '序号',
            width: 60,
            render: (text: string, record: object, idx: number) => idx + 1,
        },
        {
            title: '巡维中心',
            minWidth: 85,
            dataIndex: 'maintainCentre',
            render: (text: string) => text ?? "-"
        },
        {
            title: '安装站点',
            minWidth: 85,
            dataIndex: 'stationName',
            render: (text: string) => text ?? "-"
        },
        {
            title: '安装主变',
            minWidth: 85,
            dataIndex: 'monitorMain',
            render: (text: Array<string>) => text ? text.join("、") : "-"
        },
        {
            title: '调度编号',
            minWidth: 85,
            dataIndex: 'dispatchNumber',
            align: "center",
            render: (text: string) => text ?? "-"
        },
        {
            title: '装置厂家',
            minWidth: 85,
            dataIndex: 'deviceFac',
            render: (text: string) => text ?? "-"
        },
        {
            title: '装置型号',
            minWidth: 85,
            dataIndex: 'deviceModel',
            render: (text: string) => text ?? "-"
        },

        {
            title: '出厂编号',
            minWidth: 85,
            dataIndex: 'facNumber',
            render: (text: string) => text ?? "-"
        },

        {
            title: '投运日期',
            minWidth: 85,
            dataIndex: 'opeDate',
            render: (text: string) => text ?? "-"
        },

        {
            title: 'IP地址',
            minWidth: 70,
            dataIndex: 'ipAddress',
            render: (text: string) => text ?? "-"
        },

        {
            title: '操作',
            align: "right",
            width: 160,
            fixed: "right",
            render: (text: string, record: object, idx: number) => <>
                <span className="homePage-tableOperationBtn" onClick={() => click_operation("see", record)}>查看</span>
                {
                    $PowerSetting()["basePower"] && <>
                        <Divider className="homePage-tableOperationDivider" type="vertical" />
                        <span className="homePage-tableOperationBtn" onClick={() => click_operation("edit", record)}>编辑</span>
                        <Divider className="homePage-tableOperationDivider" type="vertical" />
                        <span className="homePage-tableOperationBtn" onClick={() => click_operation("delete", record)}>删除</span>
                    </>
                }
            </>
        },
    ];

    const http_search = () => {
        $Http("search", { typeName: "数据维护/隔直维护", ...paramsData.current, pageNo, pageSize }).then((res) => {
            setSourceData({ ...res["data"] })
        })
    }
    const click_operation = (type: string, data: object) => {
        switch (type) {
            case "delete":
                setIsDeleteModal(true)
                setModalData(data)
                break
            default:
                if (data["workOrderID"]) {
                    $Http("search", { typeName: "系统设置/审核工单", id: data["workOrderID"], processResult: 0, pageNo: 1, pageSize: 1 }).then((res) => {
                        data = { ...data, ...res["data"]["list"][0] }
                        setIsModal(true)
                        setModalData({ type, ...data })
                    })

                } else {
                    setIsModal(true)
                    setModalData({ type, ...data })
                }

                break
        }

    }

    const change_pagination = (no: number, size: number) => {
        pageNo = no
        pageSize = size
        setPageNo(pageNo)
        setPageSize(pageSize)
        http_search()
    }

    const change_export = (list: Array<number>) => {
        $export_excel("数据维护/隔直维护", list)
    }

    const change_editModal = (value: boolean) => {
        setIsModal(false)
        value && setTimeout(() => {
            $eventBus.emit("headerOperationConditionSwitch", { source: "HomePage_DataMaintenancePage_SubstationMaintenancePage", explain: "新增/编辑成功，请关闭头部操作条件，并查询", data: { isOpen: false, isSearch: true } })
        });
    }

    // 广播函数数据
    const eventBusFuncData = {
        // 头部操作新增
        headerOperationAdd: (data: object) => {
            setModalData({ type: "add" })
            setIsModal(true)
        },
        // 头部操作初始参数
        headerOperationInitParamsData: (data: object) => {


            initParamsData.current = data["data"]["initParamsData"]
            paramsData.current = data["data"]["initParamsData"]
            http_search()
        },
        // 头部操作查询
        headerOperationSearch: (data: object) => {
            pageNo = 1
            setPageNo(pageNo)
            paramsData.current = { ...paramsData.current, ...data["data"]["paramsData"] }
            http_search()
        },
        // 头部操作条件的开关
        headerOperationConditionSwitch: (data: object) => {
            pageNo = 1
            setPageNo(pageNo)
            paramsData.current = { ...initParamsData.current }
            http_search()
        },
        // 确认操作
        confirmOperation: (data: object) => {
            $Http("delete", { typeName: "数据维护/隔直维护", id: data["data"]["id"], dispatchNumber: data["data"]["dispatchNumber"] }).then((res) => {
                message.success(res["success"])
                setTimeout(() => {
                    $eventBus.emit("headerOperationConditionSwitch", { source: "HomePage_DataMaintenancePage_SubstationMaintenancePage", explain: "删除成功，请关闭头部操作条件，并查询", data: { isOpen: false } })
                })
                setIsDeleteModal(false)
            })
        }
    }



    useEffect(() => {
        $EventBus_on(eventBusFuncData)
        return () => {
            $EventBus_off(eventBusFuncData)
        }
    }, [])
    return (
        <>
            <HomePage_TableComponent name="homePageDataMaintenancePageUprightMaintenancePageTable"
                isRowSelection
                alignCenterList={["调度编号"]}
                alignRightList={["操作"]}
                columnList={columnList} pageNo={pageNo} pageSize={pageSize} sourceData={sourceData} change_pagination={change_pagination} change_selectedRowList={change_export} />
            {
                isModal && <HomePage_ModalComponent width={640} change_cancel={setIsModal} contentComponent={<HomePage_DataMaintenancePage_UprightMaintenancePage_ModalComponent sourceData={modalData} change_cancel={change_editModal} />} />
            }
            {
                isDeleteModal && <HomePage_ModalComponent width={500} change_cancel={setIsDeleteModal} contentComponent={<HomePage_ModalComponent_ConfirmationModallComponent title={`删除隔直`} content={`确认删除 “${modalData["stationName"]}” 吗？`} operation="删除" sourceData={modalData} change_cancel={setIsDeleteModal} />} />
            }
        </>
    )
}
export default HomePage_DataMaintenancePage_UprightMaintenancePage